<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>小米密码框</title>
    <style>
      .mi-form {
        display: table;
        width: 356px;
        height: 60px;
        border-radius: 4px;
        border: 1px solid rgba(0, 0, 0, 0);
        background-color: #f9f9f9;
      }

      .mi-control {
        position: relative;
        display: table-cell;
        width: 294px;
      }

      .mi-input {
        box-sizing: border-box;
        width: 100%;
        height: 60px;
        border: 0;
        padding: 30px 20px 10px;
        outline: none;
        background: none;
        appearance: none;
        font-size: 17px;
        font-family: "Noto Color Emoji";
        color: #333;
        line-height: 20px;
      }

      .mi-control label {
        user-select: none;
        position: absolute;
        top: 20px;
        left: 20px;
        height: 20px;
        font-weight: 400;
        font-size: 17px;
        color: rgba(0, 0, 0, 0.4);
        line-height: 20px;
        transition: top 0.15s cubic-bezier(0.4, 0, 0.2, 1),
          font-size 0.15s cubic-bezier(0.4, 0, 0.2, 1),
          color 0.15s cubic-bezier(0.4, 0, 0.2, 1);
        text-overflow: ellipsis;
        white-space: nowrap;
        overflow: hidden;
      }

      .mi-control label.active {
        top: 6px;
        font-size: 12px;
        color: #aaa;
      }

      .mi-password {
        display: table-cell;
        width: 60px;
        color: rgba(0, 0, 0, 0.85);
        font-size: 14px;
        vertical-align: middle;
        background: url(./images/close.png) center/30px 30px no-repeat;
        cursor: pointer;
        /* 防止选中文字 防止点击选中文本框的文字 */
        user-select: none;
      }

      .mi-password.active {
        background-image: url(./images/open.png);
      }
    </style>
  </head>

  <body>
    <div class="mi-form">
      <div class="mi-control">
        <input type="password" class="mi-input" />
        <label>密码</label>
      </div>
      <div class="mi-password"></div>
    </div>
    <script>
      const input = document.querySelector(".mi-input")
      const lb = document.querySelector("label")
      const eyes = document.querySelector(".mi-password")
      input.addEventListener("focus", () => {
        lb.classList.add("active")
      })
      input.addEventListener("blur", () => {
        if (!input.value) {
          lb.classList.remove("active")
        }
      })
      let num = 1
      eyes.addEventListener("click", () => {
        num++
        if (num % 2 === 0) {
          eyes.classList.add("active")
          input.type = "text"
        } else {
          eyes.classList.remove("active")
          input.type = "password"
        }
      })
    </script>
  </body>
</html>
